package org.example.im.client.console.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import io.netty.channel.Channel;
import org.example.im.bean.protocol.CommandType;
import org.example.im.bean.protocol.request.CreateGroupRequestPacket;
import org.example.im.client.console.ConsoleCommand;
import org.springframework.stereotype.Component;

import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.Scanner;

/**
 * @author dingrui
 * @since 2022/2/12
 * @description 创建群
 */
@Component
public class CreateGroupCommand implements ConsoleCommand {

    @Override
    public CommandType genType() {
        return CommandType.CREATE_GROUP_REQUEST;
    }

    @Override
    public void exec(Scanner s, Channel c) {
        System.out.println("[C-CreateGroup]创建群聊 拉人进群 用户id用逗号,隔开");
        String userIdStr = s.nextLine();
        if (Objects.isNull(userIdStr)) return;
        List<String> userIdList = Arrays.asList(userIdStr.split(StrUtil.COMMA));
        if (CollUtil.isEmpty(userIdList)) return;
        CreateGroupRequestPacket request = CreateGroupRequestPacket.builder().userIdList(userIdList).build();
        c.writeAndFlush(request);
    }
}
